﻿package {
	import flash.display.*;
	import flash.events.Event;
	import flash.filesystem.*;
	
	public class OpenXMLFile extends MovieClip
	{
		// public variables
		// public objects
		public var xml:XML; // Original (input) xml, and final (output) xml
		public var file:File;
		// private objects
		
		private var stream:FileStream;
		// private vars
		private var userID:String, projectID:String;
		
		/*** Load an existing file or create a new one 
		Do the following:
		1) Look for user_project.xml on desktop
		2) Look for project.xml on desktop
		3) Look for project.xml in applications folder
		4) If found in 2 or 3:
			a) If name is "build", will overwrite original file
			b) Start new test on the found project
		5) If not found open new file with project name	
		*/
		public function OpenXMLFile (userID:String, projectID:String)
		{
			this.userID = userID; this.projectID = projectID;
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			/// STEP ONE, is there a 
			// is there a file in the application directory
			file = File.applicationStorageDirectory.resolvePath(String("userfiles/"+userID+"_"+projectID+".xml"));
			trace("application storage", file.nativePath);
			if (file.exists)
			{
				trace("file exists");
				xml = openFile(file);
			}
			else
			{
				file = File.applicationDirectory.resolvePath(String("userfiles/"+userID+"_"+projectID+".xml"));
				trace("application",file.nativePath);
				if (file.exists)
				{
					xml = openFile(file);
				}
				else
				{	
					file = File.documentsDirectory.resolvePath(String("userfiles/"+userID+"_"+projectID+".xml"));
					trace("documents", file.nativePath);
					if (file.exists)
					{
						xml = openFile(file);
					}
					else
					{	
						var _file:File;
						_file = File.applicationStorageDirectory.resolvePath(String("projects/"+projectID+".xml"));
						if (_file.exists)
						{
							xml = openFile(_file);
						} else 
						{
							_file = File.applicationDirectory.resolvePath(String("projects/"+projectID+".xml"));
							if (_file.exists)
							{
								xml = openFile(_file);
							} else 
							{
								_file = File.documentsDirectory.resolvePath(String("projects/"+projectID+".xml"));
								if (_file.exists)
								{
									xml = openFile(_file);
								} else 
								{
									_file = File.applicationDirectory.resolvePath(String("projects/lesson1.xml"));
									xml = openFile(_file);
									trace("No file");
								}
							}
						}
					}
				}
			}
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		/** Open a file that is resolved to exist */
		private static function openFile(file:File):XML
		{
			var stream:FileStream = new FileStream(); 
			stream.open(file, FileMode.READ);
			var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable)); 
			stream.close();
			return(xml);			
		}
	}
}